﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using CE.User;
using CE.Global;
using CE.Provider;

public partial class Provider_CarInsurance_ViewProviderCarInsuranceSelectDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/Admin/AdminSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            ddlYear.Items.Clear();
            //int _year = CE.Global.General.GetUTCDateTime().Year;
            int _year = CE.Global.General.GetUTCDateTime().Year;
            ddlYear.Items.Add(new ListItem("All", "0000"));
            ddlYear.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            _year = _year - 1;
            ddlYear.Items.Add(new ListItem(_year.ToString(), _year.ToString()));

            if (Request.Params["CompanyId"] != null)
            {
                txtCompanyId.Text = Request.Params["CompanyId"].ToString();
                LoadProviderCoverageTypeList();
            }

            if (Request.Params["ModifiedDate"] != null)
            {
                txtModifiedDate.Text = Request.Params["ModifiedDate"].ToString();
                if (txtModifiedDate.Text.Length == 4)
                {
                    ddlYear.Text = txtModifiedDate.Text;
                }
                else if (txtModifiedDate.Text.Length == 6)
                {
                    ddlYear.Text = txtModifiedDate.Text.Substring(0, 4);
                    ddlMonth.Text = txtModifiedDate.Text.Substring(4, 2);
                }
                else if (txtModifiedDate.Text.Length == 8)
                {
                    ddlYear.Text = txtModifiedDate.Text.Substring(0, 4);
                    ddlMonth.Text = txtModifiedDate.Text.Substring(4, 2);
                    ddlDay.Text = txtModifiedDate.Text.Substring(6, 2);
                }
            }

            if (Request.Params["IsComplete"] != null)
            {
                txtIsComplete.Text = Request.Params["IsComplete"].ToString();
            }

            Button1_Click(this, null);

            //grvCarInsuranceResult.DataBind();
            //grvProfile.DataBind();
            //grvCarDetail.DataBind();
            //grvPolicy.DataBind();

            //LoadProviderList();
            //ddlProviderName.Items.Insert(0, new ListItem("All", "0"));
            //ddlProviderId.Items.Insert(0, new ListItem("All", "0"));
            //ddlDay.Text = "00";
            //ddlMonth.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
            //ddlYear.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
            //txtModifiedDate.Text = " ";
            //grvCarCapacity.DataBind();
        }
    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ViewProviderProposalSummaryReport.aspx");
    }



    //protected void LoadProviderList()
    //{
    //    ProviderCompanyLinkDB _procomdb = new ProviderCompanyLinkDB();
    //    DataTable _dtProvider = new DataTable("ProviderList");
    //    DataTable _dtList = new DataTable();
    //    _dtList.Columns.Add("Name");
    //    _dtList.Columns.Add("Email");

    //    DataTable _dtIDList = new DataTable();
    //    _dtIDList.Columns.Add("Name");
    //    _dtIDList.Columns.Add("CEID");

    //    _dtProvider = _procomdb.GetProviderList(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()));
    //    for (int i = 0; i < _dtProvider.Rows.Count; i++)
    //    {
    //        ProfileDB _profiledb = new ProfileDB();
    //        Profile _profile = new Profile();
    //        _profile = _profiledb.GetProfile(Convert.ToInt64(_dtProvider.Rows[i]["CEID"].ToString()));

    //        Contact _contact = new Contact();
    //        ContactDB _contactdb = new ContactDB();
    //        _contact = _contactdb.GetContact(Convert.ToInt64(_dtProvider.Rows[i]["CEID"].ToString()));

    //        if (_profile != null && _contact != null)
    //        {
    //            DataRow _dtRow = _dtList.NewRow();
    //            _dtRow["Name"] = _profile.Salutation + " " + _profile.SurName + " " + _profile.FirstName;
    //            _dtRow["Email"] = _contact.Email;
    //            _dtList.Rows.Add(_dtRow);

    //            DataRow _dtIDRow = _dtIDList.NewRow();
    //            _dtIDRow["Name"] = _profile.Salutation + " " + _profile.SurName + " " + _profile.FirstName;
    //            _dtIDRow["CEID"] = _dtProvider.Rows[i]["CEID"].ToString();
    //            _dtIDList.Rows.Add(_dtIDRow);
    //        }

    //        _profiledb = null;
    //        _contact = null;
    //    }
    //    ddlProviderName.DataSource = null;
    //    ddlProviderName.DataSource = _dtList;
    //    ddlProviderName.DataTextField = "Name";
    //    ddlProviderName.DataValueField = "Email";
    //    ddlProviderName.DataBind();

    //    ddlProviderId.DataSource = null;
    //    ddlProviderId.DataSource = _dtIDList;
    //    ddlProviderId.DataTextField = "Name";
    //    ddlProviderId.DataValueField = "CEID";
    //    ddlProviderId.DataBind();

    //    //ddlProviderName.Items.Insert(0, new ListItem(" ", "-1"));
    //    //ddlProviderId.Items.Insert(0, new ListItem(" ", "-1"));
    //    lblProviderEmailAddress.Text = "";
    //    //}
    //    //else
    //    //{
    //    //    ddlProviderName.DataSource = null;
    //    //    lblProviderEmailAddress.Text = "";
    //    //}
    //}

    //protected void ddlProviderName_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    if (ddlProviderName.SelectedIndex > 0)
    //    {
    //        lblProviderEmailAddress.Text = ddlProviderName.SelectedValue;
    //    }
    //    else
    //    {
    //        lblProviderEmailAddress.Text = "";
    //    }
    //    ddlProviderId.SelectedIndex = ddlProviderName.SelectedIndex;
    //}

    protected void Button1_Click(object sender, EventArgs e)
    {
        txtModifiedDate.Text = " ";
        if (ddlYear.SelectedIndex > 0)
        {
            txtModifiedDate.Text = ddlYear.Text;
            if (ddlMonth.SelectedIndex > 0)
            {
                txtModifiedDate.Text += ddlMonth.Text;
                if (ddlDay.SelectedIndex > 0)
                {
                    txtModifiedDate.Text += ddlDay.Text;
                }
                else
                {
                    ddlDay.SelectedIndex = 0;
                }
            }
            else
            {
                ddlMonth.SelectedIndex = 0;
                ddlDay.SelectedIndex = 0;
            }
        }
        else
        {
            ddlMonth.SelectedIndex = 0;
            ddlDay.SelectedIndex = 0;
        }

        txtIsComplete.Text = ddlStatus.Text;

        grvCarInsuranceResult.DataBind();

        if (grvCarInsuranceResult.Rows.Count > 0)
        {
            grvCarInsuranceResult.SelectedIndex = 0;

            grvCarInsurancePackageDetail.Visible = true;
            grvProfile.Visible = true;
            grvCarDetail.Visible = true;
            grvPolicy.Visible = true;
        }
        else
        {
            grvCarInsurancePackageDetail.Visible = false;
            grvProfile.Visible = false;
            grvCarDetail.Visible = false;
            grvPolicy.Visible = false;

            //grvCarInsurancePackageDetail.DataSource = null;
            //grvCarInsurancePackageDetail.DataBind();

            //grvProfile.DataSource = null;
            //grvProfile.DataBind();

            //grvCarDetail.DataSource = null;
            //grvCarDetail.DataBind();

            //grvPolicy.DataSource = null;
            //grvPolicy.DataBind();
        }
    }

    private void LoadProviderCoverageTypeList()
    {
        CarCoverTypeList _carcovertypelist = new CarCoverTypeList();
        CarCoverTypeDB _carcovertypedb = new CarCoverTypeDB();

        _carcovertypelist = _carcovertypedb.GetProviderCoverTypeList(Convert.ToInt64(txtCompanyId.Text));

        if (_carcovertypelist != null)
        {
            for (int i = 0; i < _carcovertypelist.Count; i++)
            {
                ddlCXCoverageType.Items.Add(new ListItem(_carcovertypelist[i].ProviderCoverTypeDescription, _carcovertypelist[i].CXCoverTypeId.ToString()));
            }
        }

        ddlCXCoverageType.Items.Insert(0, new ListItem("ALL", "0"));
    }

    protected void grvCarInsuranceResult_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblIdNo = (Label)e.Row.FindControl("lblIDNo");
            string _IdNo = Convert.ToString((DataBinder.Eval(e.Row.DataItem, "IDNO")));

            lblIdNo.Text = CE.Security.MySecurity.Decrypt(_IdNo, true);
            //lblFName.Text = _firstname;

            Label lblSName = (Label)e.Row.FindControl("lblSurName");
            string _surname = Convert.ToString((DataBinder.Eval(e.Row.DataItem, "SurName")));

            lblSName.Text = CE.Security.MySecurity.Decrypt(_surname, true);
            //lblSName.Text = _surname;

            /*
            // Medical Expense
            Label lblMedicalExpense = (Label)e.Row.FindControl("lblMedicalExpense");
            Image imgMedicalExpense = (Image)e.Row.FindControl("imgMedicalExpense");

            int _MedicalExpense = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "MedicalExpense"));

            if (_MedicalExpense == 0)
            {
                imgMedicalExpense.Visible = true;
                lblMedicalExpense.Visible = false;
                lblMedicalExpense.Text = "";
            }
            else
            {
                imgMedicalExpense.Visible = false;
                lblMedicalExpense.Visible = true;
                lblMedicalExpense.Text = _MedicalExpense.ToString("$#,000");
            }

            // Car Replacement
            Label lblCarReplacement = (Label)e.Row.FindControl("lblCarReplacement");
            Image imgCarReplacement = (Image)e.Row.FindControl("imgCarReplacement");

            bool _IsCarReplace = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "IsCarReplacement"));
            string _strCarReplace = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "CarReplacement"));

            imgCarReplacement.ImageUrl = DataBinder.Eval(e.Row.DataItem, "CarReplacementUrl").ToString();
            if (!_IsCarReplace)
            {
                lblCarReplacement.Visible = true;
                lblCarReplacement.Text = "max " + _strCarReplace + " day(s)";
            }
            else
            {
                lblCarReplacement.Visible = false;
                lblCarReplacement.Text = "";
            }

            // Cash Allowance
            Label lblCashAllowance = (Label)e.Row.FindControl("lblCashAllowance");
            Image imgCashAllowance = (Image)e.Row.FindControl("imgCashAllowance");

            int _CashAllowance = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "CashAllowance"));
            string _strCashAllowance = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "CashAllowanceText"));


            if (_CashAllowance == 0)
            {
                imgCashAllowance.Visible = true;
                lblCashAllowance.Visible = false;
                lblCashAllowance.Text = "";
            }
            else
            {
                imgCashAllowance.Visible = false;
                lblCashAllowance.Visible = true;
                lblCashAllowance.Text = _strCashAllowance;
            }
             */
        }
    }

    //protected void grvCarInsuranceResult_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    //if (grvCarInsuranceResult.SelectedIndex >= 0)
    //    //{
    //    //    //sqlDsProfile.SelectParameters["RefCEUserId"].DefaultValue = grvCarInsuranceResult.DataKeys[grvCarInsuranceResult.SelectedIndex].Values["RefCEUserId"].ToString();
    //    //    sqlDsCarDetail.SelectParameters["CECarId"].DefaultValue = grvCarInsuranceResult.DataKeys[grvCarInsuranceResult.SelectedIndex].Values["CECarId"].ToString();
    //    //    sqlDsPolicy.SelectParameters["CECarId"].DefaultValue = grvCarInsuranceResult.DataKeys[grvCarInsuranceResult.SelectedIndex].Values["CECarId"].ToString();
    //    //    //grvProfile.DataBind();
    //    //    grvCarDetail.DataBind();
    //    //    grvPolicy.DataBind();
    //    //}
    //}
    //protected void sqlDsCarDetail_Updating(object sender, SqlDataSourceCommandEventArgs e)
    //{
    //    e.Command.Parameters["CECarId"].Value = grvCarInsuranceResult.SelectedDataKey[0].ToString();
    //}
    //protected void sqlDsPolicy_Updating(object sender, SqlDataSourceCommandEventArgs e)
    //{
    //    e.Command.Parameters["CECarId"].Value = grvCarInsuranceResult.SelectedDataKey[0].ToString();
    //}

    protected void grvCarInsuranceResult_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (grvCarInsuranceResult.SelectedIndex >= 0)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            DataKey myData = grvCarInsuranceResult.DataKeys[index];

            sqlDsProfile.SelectParameters["RefCEUserId"].DefaultValue = myData[2].ToString();
            sqlDsCarInsurancePackage.SelectParameters["CISelectItemId"].DefaultValue = myData[1].ToString();
            sqlDsCarDetail.SelectParameters["CECarId"].DefaultValue = myData[0].ToString();
            sqlDsPolicy.SelectParameters["CECarId"].DefaultValue = myData[0].ToString();

            grvCarInsurancePackageDetail.DataBind();
            sqlDsProfile.DataBind();
            sqlDsCarDetail.DataBind();
            sqlDsPolicy.DataBind();
        }
    }

    protected void grvProfile_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblFName = (Label)e.Row.FindControl("lblFirstName");
            string _firstname = Convert.ToString((DataBinder.Eval(e.Row.DataItem, "FirstName")));
            if (_firstname != string.Empty)
                lblFName.Text = CE.Security.MySecurity.Decrypt(_firstname, true);
            //lblFName.Text = _firstname;

            Label lblSName = (Label)e.Row.FindControl("lblSurName");
            string _surname = Convert.ToString((DataBinder.Eval(e.Row.DataItem, "SurName")));

            lblSName.Text = CE.Security.MySecurity.Decrypt(_surname, true);
            //lblSName.Text = _surname;
        }
    }

    protected void grvCarInsuranceResult_SelectedIndexChanged(object sender, EventArgs e)
    {
        grvCarInsurancePackageDetail.DataBind();
        grvProfile.DataBind();
        grvCarDetail.DataBind();
        grvPolicy.DataBind();
    }

    protected void grvCarInsurancePackageDetail_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Medical Expense
            Label lblMedicalExpense = (Label)e.Row.FindControl("lblMedicalExpense");
            Image imgMedicalExpense = (Image)e.Row.FindControl("imgMedicalExpense");

            int _MedicalExpense = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "MedicalExpense"));

            if (_MedicalExpense == 0)
            {
                imgMedicalExpense.Visible = true;
                lblMedicalExpense.Visible = false;
                lblMedicalExpense.Text = "";
            }
            else
            {
                imgMedicalExpense.Visible = false;
                lblMedicalExpense.Visible = true;
                lblMedicalExpense.Text = _MedicalExpense.ToString("$#,000");
            }

            // Car Replacement
            Label lblCarReplacement = (Label)e.Row.FindControl("lblCarReplacement");
            Image imgCarReplacement = (Image)e.Row.FindControl("imgCarReplacement");

            bool _IsCarReplace = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "IsCarReplacement"));
            string _strCarReplace = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "CarReplacement"));

            imgCarReplacement.ImageUrl = DataBinder.Eval(e.Row.DataItem, "CarReplacementUrl").ToString();
            if (!_IsCarReplace)
            {
                lblCarReplacement.Visible = true;
                lblCarReplacement.Text = "max " + _strCarReplace + " day(s)";
            }
            else
            {
                lblCarReplacement.Visible = false;
                lblCarReplacement.Text = "";
            }

            // Cash Allowance
            Label lblCashAllowance = (Label)e.Row.FindControl("lblCashAllowance");
            Image imgCashAllowance = (Image)e.Row.FindControl("imgCashAllowance");

            int _CashAllowance = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "CashAllowance"));
            string _strCashAllowance = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "CashAllowanceText"));


            if (_CashAllowance == 0)
            {
                imgCashAllowance.Visible = true;
                lblCashAllowance.Visible = false;
                lblCashAllowance.Text = "";
            }
            else
            {
                imgCashAllowance.Visible = false;
                lblCashAllowance.Visible = true;
                lblCashAllowance.Text = _strCashAllowance;
            }

        }
    }
}
